#include <LiquidCrystal.h> // Include the LCD library
int switchRed = 9;
int switchYellow = 8;
int switchGreen = 6;
int start = 13;
//----------------------------- Declare the variable pin --------------------------------------//
float startTime = 0;
float Stop1 = 0;
float Stop2 = 0;
float Stop3 = 0;
float Stop4 = 0;
float Stop5 = 0;
float EndTime = 0;
//-----------------------------Time response --------------------------------------------------//

int wrong = 0;
int i ;
int Control =0;
int red = 0;
int yellow = 0;
int green = 0;
int sta = 0;
int ran ;
int x= 1;
int Count =0;

LiquidCrystal lcd(12, 11, 2, 3, 4, 5);   // set pin of lcd

void setup(){
Serial.begin(9600);
lcd.begin(16, 2);    // set up the LCD's number of columns and rows: 
pinMode(switchRed,INPUT);
pinMode(switchYellow,INPUT);
pinMode(switchGreen,INPUT);
pinMode(start,INPUT);
}

void loop(){
    if (digitalRead(start) == HIGH) { // push switch to start the program
        Control = 1;
        
   }
    if(Count < 5 && Control ==1){
      ran =  (int) random(65,68);
      startTime = millis();
    switch(ran){
     case 'A': // send the value = 65 to processing
        Serial.write(65);
        break;
     case 'B': // send the value = 66 to processing
        Serial.write(66);
        
        break;
     case 'C': // send the value = 67 to processing
        Serial.write(67);
        break;
    }
      while(1){
     if(ran == 65){  //Condition red color
      if(digitalRead(switchRed) == HIGH ){
       EndTime = millis();
       Serial.write(79);
       break;
      }
      else if (digitalRead(switchYellow) == HIGH || digitalRead(switchGreen) == HIGH){
         EndTime = startTime;
        Serial.write(79);
       wrong ++; 
       break; 
      }
     }
    if(ran == 66){ //Codition yellow color
       if(digitalRead(switchYellow) == HIGH){
       EndTime = millis();
       Serial.write(79);
       break;
       }
       else if (digitalRead(switchRed) == HIGH || digitalRead(switchGreen) == HIGH){
       EndTime = startTime;
       Serial.write(79);
       wrong ++; 
       break; 
      }
      } 
     if(ran == 67){ //Condition green color
      if(digitalRead(switchGreen) == HIGH){
       EndTime = millis();
       Serial.write(79);
       break;
      }
      else if (digitalRead(switchYellow) == HIGH || digitalRead(switchRed) == HIGH){
         EndTime = startTime;
       Serial.write(79);
       wrong ++; 
       break; 
      }
     }
    }
      Count++; 
      delay(500);
     }
     if(Count == 1){ // Condition time response count = 1
     Stop1 = EndTime - startTime ;
     lcd.setCursor(0,0);
     lcd.print("Time = ");
     lcd.print(Stop1/1000);
     lcd.setCursor(0,1);
     lcd.print("Wrong = ");
     lcd.print(wrong);
     }
      else if(Count == 2){ // Condition time response count = 2
     Stop2 = EndTime - startTime ;
     lcd.setCursor(0,0);
     lcd.print("Time = ");
     lcd.print(Stop2/1000);
     lcd.setCursor(0,1);
     lcd.print("Wrong = ");
     lcd.print(wrong);
     }
      else if(Count == 3){ // Condition time response count = 3
     Stop3 = EndTime - startTime ;
     lcd.setCursor(0,0);
     lcd.print("Time = ");
     lcd.print(Stop3/1000);
     lcd.setCursor(0,1);
     lcd.print("Wrong = ");
     lcd.print(wrong);
     }
      else if(Count == 4){ // Condition time response count = 4
     Stop4 = EndTime - startTime ;
     lcd.setCursor(0,0);
     lcd.print("Time = ");
     lcd.print(Stop4/1000);
     lcd.setCursor(0,1);
     lcd.print("Wrong = ");
     lcd.print(wrong);
     }
     else if(Count == 5){ // Condition time response count = 5
     Stop5 = EndTime - startTime ;
     lcd.setCursor(0,0);
     lcd.print("Time = ");
     lcd.print(Stop5/1000);
     lcd.setCursor(0,1);
     lcd.print("Wrong = ");
     lcd.print(wrong);
     Count ++;
     }

     
  }



 
  
  
  

